<?php
App::uses('AppController', 'Controller');
/**
 * Venues Controller
 *
 * @property Venue $Venue
 * @property PaginatorComponent $Paginator
 */
class VenuesController extends AppController {

	public $usefor = array(
				'1'=>'演唱会',	'2'=>'音乐会',	'3'=>'话剧',
				'4'=>'歌剧',		'5'=>'戏曲综艺',	'6'=>'魔术马戏',
				'7'=>'舞蹈芭蕾',	'8'=>'体育比赛',	'9'=>'儿童亲子',
				'10'=>'其他'
			);
/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
	
	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('index','view');
	}
	
	/**
	 * 场馆列表
	 * @author mingxing.zhao@qq.com
	 * @date 2014-1-7 下午7:44:23
	 * @param variable
	 * @return 
	 */
	public function index() {
		$this->Venue->recursive = 0;
		$SITEID = Configure::read('SITEID');
		$this->request->named = parseSeoParam($this->request->params['pass']);
		$conditions = $this->indexSearch();
		$this->Paginator->settings = array(
				'conditions'=>	$conditions['conditions'],
				'order'=>$conditions['order'],
				'limit'=>16
		);
		
		$venues = $this->Paginator->paginate('Venue');
		$this->set('usefor',$this->usefor);
		$this->set('venues', $venues);
	}
	
	private function indexSearch(){
		$usefor = isset($this->request->named['usefor'])?$this->request->named['usefor']:false;
	
		$SITEID = Configure::read('SITEID');
		$conditions = array();
		$conditions['Venue.sys_website_id'] = $SITEID;
		$conditions['Venue.enable'] = 1;
	
		if($usefor){
			$conditions['Venue.usefor like '] = '%,'.$usefor.',%';
		}
	
		$sort = 'Venue.sort DESC';
		$sparams = array('usefor'.$usefor);
		$this->set('sparams',$sparams);
		if($usefor){
			$this->set('seo_title',$this->usefor[$usefor].'场馆演出');
		}else{
			$this->set('seo_title','全部场馆演出');
		}
		
		$this->set('seo_title_ext',' - 【秦文票务】');
		$this->set('seo_keywords',false);
		$this->set('seo_description',false);
		return array('conditions'=>$conditions,'order'=>$sort);
	}
	
	/**
	 * 场馆列表
	 * @author mingxing.zhao@qq.com
	 * @date 2014-1-7 下午7:44:23
	 * @param variable
	 * @return
	 */
	public function view($id=null) {
		if (!$this->Venue->exists($id)) {
			throw new NotFoundException(__('Invalid venue'));
		}
		
		$this->loadModel('Ticket');
		$count = $this->Ticket->find('count',array(
				'conditions' => array(
					'Ticket.date_end >'=>date('Y-m-d'),
					'Ticket.venue_id'=>$id
		)));
		$this->Venue->id = $id;
		$this->Venue->set('count_tickets',$count);
		$this->Venue->save();
		
		
		$this->Venue->bindModel(array(
				'hasMany'=>array(
						'Ticket' => array(
							'className' => 'Ticket',
							'foreignKey' => 'venue_id',
							'dependent' => false,
							'conditions' => array(
								'Ticket.date_end >'=>date('Y-m-d'),
							     'Ticket.enable'=>1     
							),
							'fields' => '',
							'order' => 'Ticket.date_end DESC,Ticket.sort_top DESC,Ticket.showtime DESC',
							'limit' => 4,
							'offset' => '',
							'exclusive' => '',
							'finderQuery' => '',
							'counterQuery' => ''
						)
				)
		));
		$this->Venue->recursive =2;
		$options = array('conditions' => array('Venue.' . $this->Venue->primaryKey => $id));
		$venue = $this->Venue->find('first', $options);
		
		$this->set('venue', $venue);
		
		// 分类-------------------------------------------------------------
		$this->loadModel('TicketCate');
		$SITEID = Configure::read('SITEID');
		$siteTicketCate = $this->TicketCate->getSiteEnable($SITEID);
		$this->set('siteTicketCate',$siteTicketCate);
		
		$seo_title		 = $venue['SysSeo']['title'];
		$seo_keywords	 = $venue['SysSeo']['keywords'];
		$seo_description = $venue['SysSeo']['description'];
		$this->set('seo_title',$seo_title);
		$this->set('seo_keywords',$seo_keywords);
		$this->set('seo_description',$seo_description);
		$this->set('seo_title_ext','最新演出订票【秦文票务】');
		
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Venue->recursive = 0;
		$this->set('venues', $this->Paginator->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Venue->exists($id)) {
			throw new NotFoundException(__('Invalid venue'));
		}
		$options = array('conditions' => array('Venue.' . $this->Venue->primaryKey => $id));
		$this->set('venue', $this->Venue->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Venue->create();
			
			$this->request->data['Venue']['usefor'] = ','.join(',',$this->request->data['Venue']['usefor']).',';
			
			if ($this->Venue->save($this->request->data)) {
				$this->Session->setFlash(__('场馆添加成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				if($this->request->data['Venue']['sys_photo_id']){
					$photo = $this->Venue->SysPhoto->findById($this->request->data['Venue']['sys_photo_id']);
					$this->request->data['SysPhoto'] = $photo['SysPhoto'];
				}
				$this->request->data['Venue']['usefor'] = split(',', $this->request->data['Venue']['usefor']);
				$this->Session->setFlash(__('场馆添加失败，请重试'),'tip/tip_float');
			}
		}
		$this->set('usefor',$this->usefor);
		$this->loadModel('SysWebsite');
		$SessionSite = $this->SysWebsite->findById($this->Session->read('SITEID'));
		$this->set('city_id',$SessionSite['SysWebsite']['sys_city_id']);
		$sysAreas = $this->Venue->SysArea->findAreaList($SessionSite['SysWebsite']['sys_city_id']);
		$this->set(compact('sysCities', 'sysAreas'));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Venue->exists($id)) {
			throw new NotFoundException(__('Invalid venue'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if(!empty($this->request->data['Venue']['usefor']))
			$this->request->data['Venue']['usefor'] = ','.join(',',$this->request->data['Venue']['usefor']).','; 
			
			if ($this->Venue->save($this->request->data)) {
				$this->Session->setFlash(__('场馆修改成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				if($this->request->data['Venue']['sys_photo_id']){
					$photo = $this->Venue->SysPhoto->findById($this->request->data['Venue']['sys_photo_id']);
					$this->request->data['SysPhoto'] = $photo['SysPhoto'];
				}
				$this->request->data['Venue']['usefor'] = split(',', $this->request->data['Venue']['usefor']);
				$this->Session->setFlash(__('场馆修改失败，请重试'),'tip/tip_float',array('t'=>'error'));
			}
		} else {
			$options = array('conditions' => array('Venue.' . $this->Venue->primaryKey => $id));
			$data = $this->Venue->find('first', $options);
			$data['Venue']['usefor'] = split(',', $data['Venue']['usefor']);
			$this->request->data = $data;
		}
		$this->set('usefor',$this->usefor);
		$this->loadModel('SysWebsite');
		$sysAreas = $this->Venue->SysArea->findAreaList($this->request->data['Venue']['sys_city_id']);
		$this->set(compact('sysCities', 'sysAreas'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Venue->id = $id;
		if (!$this->Venue->exists()) {
			throw new NotFoundException(__('Invalid venue'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Venue->delete()) {
			$this->Session->setFlash(__('The venue has been deleted.'));
		} else {
			$this->Session->setFlash(__('The venue could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
